<?php
/**
 * Jobs Display on edition mode
 *
 * @author Xavier VILAIN
 */
use Hive\Job\Contract\Type;
use Fox\Ddic\DataElement;
use Fox\Html\Action\Anchor;
use Fox\Behaviors;
use Hive\Ressource;
use Hive\Ressource\Experience;
use Fox\Html\Header\Content;
use Fox\Html\Screen,Fox\Generator,Fox\Html\Button;
use Fox\Html\Block,Fox\Core\Errors;

$xview = Xview::instance();
/* @var $contact Hive\Ressource */
$contact = $xview->component('ressource','object');
// Customizing
$customizing = Hive\Ressource\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'ressource','act'=>'jobs'));
$rid = $xview->val('rid');

$currency = Hive\Customizing::instance()->defaultCurrency();

$header = new Content('jobs',Msg(Hive::BUNDLE,Hive::MENU_INFO_JOB));
$header->add(new Button('add','?',Msg(Hive::BUNDLE,Hive::MSG_ADD_A_NEW_JOB)))
		->color(Button::COLOR_WARNING)
		->behaviors(Behaviors::action(
				Generator::url('ressource','jobs','add',array('rid' => $contact->getId())),
				true,null,null,true));
$behaviorSource = new Behaviors(Behaviors::source(Generator::url('ressource','jobs',null,array('rid' => $rid))));
$htmlExp = null;
foreach($contact->experiences() as $object) {
	$jid = $object->getId();
	$data = $object->getData();
	$companyName = $object->companyName();
	$jobTitle = $object->jobtitle;
	// Job Period - Left Column
	$timePart = null;
	if(empty($data['end_date'])) {
		// It means this is a current job position
		$timePart .= '<div class="stillin">'.Msg(Hive::BUNDLE,Hive::TODAY).'</div>';
	} else {
		$timePart .= '<div class="period-end">'.Fox\Utilities\Dates::dateTimeToMonthYearString($data['end_date']).'</div>';
	}
	if(!empty($data['start_date'])) {
		$timePart .= '<div class="period-start">'.Fox\Utilities\Dates::dateTimeToMonthYearString($data['start_date']).'</div>';
	}
	$timePart = '<div class="experience-period"><div class="period">'.$timePart.'</div></div>';
	// Right Column - Job Information
	if(!empty($data['contract_type'])) {
		$jobTitle .= '<span class="contract-type"> ['.Type::labelize($data['contract_type']).']</span>';
	}
	// Add Job Actions
	// Update
	$behaviorUpdate = new Behaviors(Behaviors::action(Generator::url('ressource','jobs','update',
								array('rid' => $rid,'jid' => $jid)),true,null,null,true));
	$behaviorUpdate->add(Behaviors::value('slink'));
	// Delete
	$behaviorDelete = new Behaviors(Behaviors::action(Generator::url('ressource','jobs','delete',
								array('inlineid' => $object->getInLineId())),true,null,null,true));
	$behaviorDelete->add(Behaviors::value('slink del'));
	
	$jobTitle .= '<span class="uiActionLinks">
						&#8226;
						<a href="#" id="upd_'.$jid.'" class="'.$behaviorUpdate->serialize(array(),'').'">'.Msg(Hive::BUNDLE,Hive::UPDATE).'</a>
						 &#8226; <a href="#" id="del_'.$jid.'" class="'.$behaviorDelete->serialize(array(),'').'">'.Msg(Hive::BUNDLE,Hive::DELETE).'</a>
					</span>';
	$jobDetail = !empty($jobTitle) ? '<p class="jobtitle">'.$jobTitle.'</p>' : null;
	// Company Information
	$jobDetail .= !empty($companyName) ? '<p class="company-info"><a href="?mod=company&act=main&cpid='.$object->compid.'" >'.$companyName.'</a></p>' : null;
	// Current Salary
	if(!empty($data['salary'])) {
		$jobDetail .= '<p class="wadges">';
		if(!empty($data['salary'])) {
			$jobDetail .= '<span class="salary">'.DataElement\Text::label('salary').' '.$data['salary'].' '.$currency->symbol().'</span>';
		}
		if(!empty($data['bonus'])) {
			$jobDetail .= !empty($data['salary']) ? ' + ' : null;
			$jobDetail .= '<span class="bonus">'.DataElement\Text::label('bonus').' '.$data['bonus'].' '.$currency->symbol().'</span>';
		}
		$jobDetail .= '</p>';
	}
	if(!empty($data['comment'])) {
		$jobDetail .= '<p class="comments">'.Fox\Utilities\Text::toHtml($data['comment']).'</p>';
	}
	$htmlExp[] = '<li>'.$timePart.'<div class="experience-detail"><div class="detail">'.$jobDetail.'</div></div></li>';
}

if(count($contact->experiences()) > 0) {
	$htmlExp = '<ul>'.implode('',$htmlExp).'</ul>';
	$html = '<div class="job-experiences '.$behaviorSource->serialize(array(),'').'">'.$htmlExp.'</div>';
}
else {
	$html = '<p class="prettyprint">'.Msg(Hive::BUNDLE,Hive::MSG_NO_EXPERIENCES).'</p>';
}
?>
<div>
	<div class="page-header clearfix">
		<?php echo $header->render();?>
	</div>
	<div class="contentArea">
		<div class="mbs ">
			<?php echo $html;?>
		</div>
	</div>
</div>